package com.tuling.learnmongodb.service.impl;

import com.alibaba.fastjson.JSON;
import com.tuling.learnmongodb.model.Blog;
import com.tuling.learnmongodb.model.Comment;
import com.tuling.learnmongodb.model.Order;
import com.tuling.learnmongodb.model.Reply;
import com.tuling.learnmongodb.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class BlogServiceImpl implements BlogService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Blog> queryBlogList() {
        LookupOperation commentLookupOperation = LookupOperation.newLookup().
                from("comments").
                localField("_id").
                foreignField("blogId").
                as("commentList");

        List<AggregationOperation> operations = new ArrayList<>();
        operations.add(commentLookupOperation);


        Aggregation aggregation = Aggregation.newAggregation(operations);
        AggregationResults<Blog> results = mongoTemplate.aggregate(aggregation, "blogs", Blog.class);

        List<Blog> mappedResults = results.getMappedResults();
        log.info("queryBlogList:{}", JSON.toJSONString(mappedResults));
        return mappedResults;
    }

    @Override
    public void addBlog(Blog blog) {
        blog.setCreatedTime(new Date());
        blog.setId(UUID.randomUUID().toString());
        mongoTemplate.insert(blog, "blogs");
        log.info("insert success:{}", JSON.toJSONString(blog));
    }

    @Override
    public void addBlogComment(Comment comment) {
        Query query = new Query(Criteria.where("_id").is(comment.getBlogId()));
        Blog blog = mongoTemplate.findOne(query, Blog.class, "blogs");
        if(blog == null){
            throw new RuntimeException("说说不存在，评论失败!");
        }
        comment.setId(UUID.randomUUID().toString());
        comment.setCreatedTime(new Date());
        mongoTemplate.insert(comment, "comments");
        log.info("addBlogComment success:{}", JSON.toJSONString(comment));
    }

    @Override
    public void addReply(Reply reply) {
        Query query = new Query(Criteria.where("_id").is(reply.getCommentId()));
        Comment comment = mongoTemplate.findOne(query, Comment.class, "comments");
        if(comment == null){
            throw new RuntimeException("评论不存在，回复失败!");
        }
        // 初始化 update 对象
        Update update = new Update();
        reply.setCreatedTime(new Date());
        // 追加回复
        update.push("replyList", reply);
        // 更新订单信息
        mongoTemplate.upsert(query, update, Comment.class, "comments");
    }
}
